// pages/vehicle/chachediaodu_nav/jindu_search/jindu_search.js
const mybehavior = require("../../../../../behaviors/requests")
import Dialog from '@vant/weapp/dialog/dialog';
const options11 = {
  K1: ['8号电梯口', '12号电梯口',],
  K2: ['30号电梯口', '45号电梯口', '定额库'],
  K3: ['96号电梯口',],
  K4: ['东南门', '九期东南角', '水箱4号点击'],
  SZ: ['104号电梯口',],
};
Page({
  behaviors: [mybehavior],

  /**
   * 页面的初始数据
   */
  data: {
    base_urlstr: "https://logisticsengineering.gree.com/order_system",
    popup_date_show: false,
    currentDate: new Date().getTime(),
    popup_date_show1: false,
    start_time_str: "",
    end_time_str: "",
    activeName: ['0'],
    create_time: "",
    receiver_num: "",
    starting_point: "",
    end_point: "",
    list: [
    ],
    popup_show0: false,
    options0: ["未接单", "已接单", "已完结"],
    creator_id: "",
    page_size: 5,
    page: 1,
    total: 0,
    page_all: 0,
    pickup_person_name: "",
    show3: false,
    row_data: {},
    receiver_num: "",
    receiver: "",
    task_quantity: 1,
    task_quantity_max: "",
    popup_show0: false,
    popup_show1: false,
    options0: ["15号电梯", "18号电梯",],

    options1: [
      {
        values: Object.keys(options11),
      },
      {
        values: options11['K1'],
      },
    ],

  },
    // 点击--配送起点--输入框
    onClick0(event) {
      // event.detail 为当前输入的值
      // console.log(event.detail);
      this.setData({
        popup_show0: true
      })
    },
    // 选择--配送起点--输入框关闭
    onClose0() {
      this.setData({ popup_show0: false });
    },
    // 确定-配送起点--弹出框关闭
    onChange0(event) {
      const { picker, value, index } = event.detail;
      this.setData({
        starting_point: value,
        popup_show0: false,
      })
    },
      // 点击--配送终点--输入框
  onClick1(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show1: true
    })
  },
  // 选择--配送终点--输入框关闭
  onClose1() {
    this.setData({ popup_show1: false });
  },
  onChange1(event) {
    console.log("111", event);
    const { picker, value, index } = event.detail;
    picker.setColumnValues(1, options11[value[0]]);
  },
  // 确定-配送终点--弹出框关闭
  onConfirm1(event) {
    const { picker, value, index } = event.detail;
    let val = event.detail.value[0] + "-" + event.detail.value[1]
    this.setData({
      end_point: val,
      popup_show1: false,
    })
  },
  // 分页
  onPageChange(e) {
    // console.log("xxx",e.detail.sonParam);
    let p = e.detail.sonParam
    this.setData({
      page: p,
    })
    this.getDataList()
  },
  // 条件查询
  searchBtn() {
    let t = []
    if (this.data.start_time_str) {
      t.push(this.data.start_time_str)

    }
    if (this.data.end_time_str) {
      t.push(this.data.end_time_str)

    }
    let url = this.data.base_urlstr + "/query_transfer_task_list"
    let data = {
      create_time: t,
      receiver_num: this.data.receiver_num,
      starting_point: this.data.starting_point,
      end_point: this.data.end_point,
    }
    this.post(url, data).then(res => {
      if (res.code == 20000) {
        console.log(res);
        let show_list = res.data

        this.setData({
          list: show_list,
          // total: res.data.total,
          activeName: [],
          // page_all: Math.ceil(res.data.total / this.data.page_size)
        })

      } else {
        this.setData({
          list: []
        })
        wx.showToast({
          title: res.msg,
          icon: "error"
        })
      }
    }).catch(err => {
      this.setData({
        list: []
      })
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },
  // 确定日期1输入框
  onDateConfirm(e) {
    this.setData({
      popup_date_show: false,
      start_time_str: this.formatDate1(new Date(e.detail)),
    });
  },
  // 日期1输入框关闭
  onDateClose() {
    this.setData({
      popup_date_show: false,
    });
  },
  // 点击输入框显示选择日期1
  onDateClick() {
    this.setData({
      popup_date_show: true,
    });
  },


  // 确定日期2输入框
  onDateConfirm1(e) {
    this.setData({
      popup_date_show1: false,
      end_time_str: this.formatDate1(new Date(e.detail)),
    });
  },
  // 日期2输入框关闭
  onDateClose1() {
    this.setData({
      popup_date_show1: false,
    });
  },
  // 开始点击输入框显示选择日期2
  onDateClick1() {
    this.setData({
      popup_date_show1: true,
    });
  },

  // 获取数据
  getDataList() {
    let url = this.data.base_urlstr + "/query_transfer_task_list"
    let data = {
      create_time: this.data.create_time,
      receiver_num: this.data.receiver_num,
      starting_point: this.data.starting_point,
      end_point: this.data.end_point,
    }
    this.post(url, data).then(res => {
      if (res.code == 20000) {
        console.log(res);
        let show_list = res.data

        this.setData({
          list: show_list,
          // total: res.data.total,
          // activeName: [],
          // page_all: Math.ceil(res.data.total / this.data.page_size)
        })

      } else {
        this.setData({
          list: []
        })
        wx.showToast({
          title: res.msg,
          icon: "error"
        })
      }
    }).catch(err => {
      this.setData({
        list: []
      })
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  // 点接单按钮
  acceptTask(e) {
    console.log(e.currentTarget.dataset.row);

    this.setData({
      show3: true,
      row_data: e.currentTarget.dataset.row,
      task_quantity_max: e.currentTarget.dataset.row.unprocessed_quantity
    })
  },
  onClose3() {
    this.setData({
      show3: false
    })
  },
  // 数量按钮
  onStepperChange(value) {
    this.setData({
      task_quantity: value.detail
    });
  },


  // 确认接单按钮
  onConfirm3() {
    let row = this.data.row_data
    let url = this.data.base_urlstr + "/receiver_accept_transfer_task"
    let data = {
      starting_point: row.starting_point,
      end_point: row.end_point,
      receive_quantity: this.data.task_quantity,
      receiver_num: this.data.receiver_num,
      receiver: this.data.receiver,
      create_time: [],
    }
    this.post(url, data).then(res => {
      if (res.code == 20000) {
        this.getDataList()
        this.setData({
          receive_quantity:1,
          task_quantity:1
        })
        wx.showToast({
          title: res.msg,
          icon: 'success'
        })

      } else {

        wx.showToast({
          title: res.msg,
          icon: "error"
        })
      }
    }).catch(err => {

      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },

  // 报完工
  bwgBtn(e) {
    console.log(e.currentTarget.dataset.row);
    Dialog.confirm({
      title: '确认报完工？',
    })
      .then(() => {
        let url = this.data.base_urlstr + "/receiver_complete_task"
        let data = {
          id: e.currentTarget.dataset.row._id,

        }
        this.post(url, data).then(res => {
          if (res.code == 20000) {
            this.getDataList()
            wx.showToast({
              title: res.msg,
              icon: 'success'
            })

          } else {

            wx.showToast({
              title: res.msg,
              icon: "error"
            })
          }
        }).catch(err => {

          wx.showToast({
            title: err.msg,
            icon: 'error'
          })
        })
      })
      .catch(() => {
        // on cancel
      });

  },



  onChange(event) {
    this.setData({
      activeName: event.detail,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      receiver_num: wx.getStorageSync('_id'),

      receiver: wx.getStorageSync('name')
    })
    this.getDataList()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})